package service.impl;


import entity.Roleurl;
import entity.User;
import entity.UserRole;
import mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import service.Userservice;
import utils.RedisUtil;
import utils.error.BusinessException;
import utils.error.EmBusinessError;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class Userserviceimpl implements Userservice {
    private static int Expirettime=180;
    @Resource
    UserMapper userMapper;

    @Resource
    RedisUtil redisUtil;


    @Override
    public List<User> findall() {
        List<User> users = userMapper.indexselect();
        redisUtil.lSet("userlist",users);
        redisUtil.expire("userlist",Expirettime);
        return users;
    }

    @Override
    public User findeid(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insert(User user) {
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Password);
        return userMapper.insert(user);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {

        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) throws BusinessException {
        Integer id = record.getId();
        if (id == null)
        {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }else {
            if (this.findeid(id).equals(null))
            {
                throw new BusinessException(EmBusinessError.USER_NOT_UPDATE);
            }else
                {
                    String md5Password = DigestUtils.md5DigestAsHex(record.getPassword().getBytes());
                    record.setPassword(md5Password);
                    return userMapper.updateByPrimaryKeySelective(record);
                }
        }
    }

    @Override
    public int updateByPrimaryKey(User record) throws BusinessException {
        Integer id=record.getId();
        if (id==null)
        {
            if (this.findeid(id).equals(null))
            {
                return userMapper.updateByPrimaryKey(record);
            }else {
                throw new BusinessException(EmBusinessError.USER_NOT_UPDATE);
            }
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
    }

    @Override
    public User login(String username, String password) {
        String newpassword = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        User userbyid = userMapper.login(username,newpassword);
        return userbyid;
    }

    @Override
    public List<UserRole> listroleid() {
        return userMapper.listroleid();
    }

    @Override
    public List<Roleurl> urllist() {
        return userMapper.urllist();
    }

    @Override
    public Roleurl urlpid(String url) {
        return userMapper.urlpid(url);
    }

}
